package com.lhh.userservice.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhh.springcommon.annotation.WebLog;
import com.lhh.springcommon.entity.Comment;
import com.lhh.springcommon.entity.vo.CommentVO;
import com.lhh.springcommon.mapper.CommentMapper;
import com.lhh.springcommon.service.ICommentService;
import com.lhh.springcommon.utils.PageQuery;
import com.lhh.userservice.service.UserCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 李厚航
 * @date 2025-03-01 22:02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserCommentServiceImpl implements UserCommentService {

    private final ICommentService commentService;

    private final CommentMapper commentMapper;

    @Autowired
    public UserCommentServiceImpl(ICommentService commentService,
                                  CommentMapper commentMapper) {
        this.commentService = commentService;
        this.commentMapper = commentMapper;
    }

    @Override
    @WebLog
    @CacheEvict(value = "queryPageComment",allEntries = true)
    public Boolean delete(Comment comment) {
        return commentService.remove(new LambdaQueryWrapper<Comment>()
                .eq(comment.getId()!=null,Comment::getId,comment.getId())
                .eq(comment.getUid()!=null,Comment::getUid,comment.getUid())
                .eq(comment.getVid()!=null,Comment::getVid,comment.getVid())
                .eq(comment.getCid()!=null,Comment::getCid,comment.getCid())
                .eq(comment.getUserId()!=null,Comment::getUserId,comment.getUserId()));
    }

    @Override
    @WebLog
    @CacheEvict(value = "queryPageComment",allEntries = true)
    public CommentVO add(Comment comment) {
        commentService.save(comment);
        Comment com = new Comment();
        if(comment.getCid()!=0){
            com.setId(comment.getCid());
        }else{
            com.setId(comment.getId());
        }
        //获取一级评论
        CommentVO commentVO = commentMapper.queryPageComment(com, new Page<>(1, 1)).getRecords().get(0);
        commentVO.setTwoCommentVOList(commentMapper.queryPageTwoComment(commentVO, new Page<>(1, 100)).getRecords());
        return commentVO;
    }

    @Override
    @WebLog
    @Cacheable(value = "queryPageComment",key = "#comment.page + ':' + #comment.pageSize + ':' + #comment.queryInfo.page + ':' + #comment.queryInfo.pageSize + ':' + #comment.queryInfo.queryInfo.vid")
    public Page<CommentVO> queryPageComment(PageQuery<PageQuery<Comment>> comment) {
        //获取二级评论信息
        Page<CommentVO> pageTwo = new Page<>(comment.getPage(), comment.getPageSize());
        //获取一级评论信息
        Page<Comment> page = new Page<>(comment.getQueryInfo().getPage(), comment.getQueryInfo().getPageSize());
        Comment queryInfo = comment.getQueryInfo().getQueryInfo();
        //查询一级评论
        Page<CommentVO> result = commentMapper.queryPageComment(queryInfo, page);
        //查询二级评论
        result.getRecords().forEach(commentVO -> {
            commentVO.setTwoCommentVOList(commentMapper.queryPageTwoComment(commentVO, pageTwo).getRecords());
        });
        return result;
    }

}
